package cn.arnold.offer.guanglianda.test3;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Author Arnold
 * @Desc 排序算法定义如下：每次只能把一个元素提到序列的开头，最少需要多少次操作才可以使得原序列从小到大排序
 **/
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();
        }
        System.out.println(fun(n, array));
    }

    public static int fun(int n, int[] array) {
        int[] sortArray = Arrays.copyOf(array, n);
        Arrays.sort(sortArray);
        int p = n - 1, q = n - 1;
        while (p >= 0 && q >= 0) {
            if (array[p] == sortArray[q]) {
                --p;
                --q;
            } else {
                while (p >= 0 && array[p] != sortArray[q]) {
                    --p;
                }
            }
        }
        return q + 1;
    }
}
